package framework;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class HealthBar extends BasicEntity {

	protected int percentage;

	protected BasicFighter fighter;
	
	protected final static int EFFECT_LENGTH = 250;

	protected final static int DEFAULT_WIDTH = 450;

	protected final static int DEFAULT_HEIGHT = 45;

	protected final static int SPACE = 10;

	protected final static int BORDER = 7;
	
	protected Image barImg;
	
	protected Image healthImg;
	
	protected Image borderImg;

	protected int lastHealth;
	
	protected int timeEffect;

	public HealthBar(BasicFighter f) {
		this.fighter = f;
		setWidth(DEFAULT_WIDTH);
		setHeight(DEFAULT_HEIGHT);
		lastHealth = 100;
		timeEffect = -1;
		try {
			barImg = new Image(C.ASSETS_FOLDER + "gui" + "/healthbar.png");
			healthImg = new Image(C.ASSETS_FOLDER + "gui" + "/health.png");
			borderImg = new Image(C.ASSETS_FOLDER + "gui" + "/healthbar_border.png");
		} catch (SlickException e) {
			e.printStackTrace();
		}
	}

	public void draw(Graphics g) {
		AngelCodeFont font = FontManager.getLittle();
		if (this.fighter != null) {
			int b = (int) (BORDER * getScale());
			// drawing teleports
			g.setColor(Color.gray);
			g.fillRect(getX() + getWidth() - fighter.getMaxNbTeleport() * 15 - 30, getY() + getHeight(), 15 * fighter.getMaxNbTeleport(), 15);
			g.setColor(new Color(125, 125, 255));
			g.fillRect(getX() + getWidth() - fighter.getMaxNbTeleport() * 15 - 30, getY() + getHeight(), 15 * fighter.getNbTeleport(), 15);
			g.setColor(Color.black);
			for(int i=1; i <= fighter.getMaxNbTeleport(); i++) {
				g.drawRect(getX() + getWidth() - i * 15 - 30, getY() + getHeight(), 15, 15);
			}
			
			g.setFont(font);
			g.setColor(Color.white);
			g.drawString(this.fighter.getFighterName(), getX() + SPACE * getScale(), getY() + getHeight() + SPACE * getScale() / 2);
			
			// drawing health bars);
			barImg.draw(getX(), getY(), getWidth(), getHeight());

			int healthwidth = healthImg.getWidth() - Math.max(0, fighter.getHealthPoint() * healthImg.getWidth() / 100);
			Image sub = healthImg.getSubImage(0, 0, healthwidth, healthImg.getHeight());
			sub.draw(getX(), getY(), getWidth() - Math.max(0, fighter.getHealthPoint() * getWidth() / 100), getHeight());
			borderImg.draw(getX(), getY(), getWidth(), getHeight());
		}
	}

	@Override
	public void update(GameContainer gc, int delta) {
		if (fighter.getHealthPoint() != lastHealth && timeEffect == -1) {
			System.out.println("pls");
			timeEffect = 0;
		}
		
		if (timeEffect > -1 && timeEffect < EFFECT_LENGTH) {
			timeEffect += delta;
		} 
		if (timeEffect >= EFFECT_LENGTH) {
			timeEffect = -1;
			lastHealth = fighter.getHealthPoint();
		}
	}
}